﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using BUS;
using DTO;

namespace FastDeal.DoanhNghiep
{
    public partial class KhuyenMaiTheoDoanhNghiep : System.Web.UI.Page
    {
        private KhuyenMai_BUS khuyenMai_BUS = new KhuyenMai_BUS();
        private ChiTiet_HoaDon_BUS ct_HoaDonBUS = new ChiTiet_HoaDon_BUS();
        private HoaDon_BUS hd_Bus = new HoaDon_BUS();
        KhuyenMai km = new KhuyenMai();
        private int k = 0;
        public void HienThiThongTin()
        {
            int tenKhuyenMai = int.Parse(Request.QueryString["MaKhuyenMai"].ToString());
            List<ChiTiet_HoaDon> dsCTHoaDon = new List<ChiTiet_HoaDon>();
            dsCTHoaDon = ct_HoaDonBUS.LayThongTin_CTHDTheoMaKhuyenMai(tenKhuyenMai);
            string tenKM = khuyenMai_BUS.KhuyenMai_MaKhuyenMai(tenKhuyenMai).TenKhuyenMai;
            lb_TenKhuyenMai.Text = tenKM;
            List<HoaDon> dsHoaDon = new List<HoaDon>();
            List<HoaDon> dsHoaDon_maKhuyenMai = new List<HoaDon>();
            dsHoaDon = hd_Bus.LayDanhSach_HoaDon();
            double tongTien = 0;
            foreach (ChiTiet_HoaDon ct in dsCTHoaDon)
            {
                foreach (HoaDon hd in dsHoaDon)
                {
                    if (ct.MaHoaDon == hd.MaHoaDon)
                    {
                        HoaDon hoaDon = new HoaDon();
                        hoaDon.MaHoaDon = ct.MaHoaDon;
                        hoaDon.NgayDatMua = hd.NgayDatMua;
                        hoaDon.UserID = hd.UserID;
                        hoaDon.TongTien = (ct.SoLuong * ct.GiaKhuyenMai);
                        hoaDon.DiaChiGiaoHang = hd.DiaChiGiaoHang;
                        dsHoaDon_maKhuyenMai.Add(hoaDon);
                        tongTien = tongTien + (ct.SoLuong * ct.GiaKhuyenMai);
                    }
                }
            }
            lb_tongDoanhThu.Text = tongTien.ToString();
            GV_DSHoaDon.AutoGenerateColumns = false;
            GV_DSHoaDon.DataSource = dsHoaDon_maKhuyenMai;
            GV_DSHoaDon.DataBind();
            if (dsHoaDon_maKhuyenMai.Count > 0)
            {
                for (int i = 0; i <  dsHoaDon_maKhuyenMai.Count -1; i++)
                {
                    if (dsHoaDon_maKhuyenMai[i].TinhTrang ==0)
                    {
                        GV_DSHoaDon.Rows[i].Cells[1].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[2].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[3].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[4].BackColor = Color.Goldenrod;
                        GV_DSHoaDon.Rows[i].Cells[5].BackColor = Color.Goldenrod;
                    }
                }
            }
            if (Panel1.Height.Value < 800)
            {
                Panel1.Height = 1000;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 3)
            {
                Response.Redirect("~/Default.aspx");
            }
            HienThiThongTin();
        }

        protected void GV_DSHoaDon_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_DSHoaDon.EditIndex = e.NewEditIndex;
            HienThiThongTin();
        }

        protected void GV_DSHoaDon_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = int.Parse(((Label)GV_DSHoaDon.Rows[e.RowIndex].Cells[0].FindControl("lb_MaHoaDon")).Text);
            HoaDon hd = hd_Bus.LayThongTin_HoaDon(index);
            hd.TinhTrang = 1;
            hd_Bus.Sua_HoaDon(hd);
            GV_DSHoaDon.EditIndex = -1;
            HienThiThongTin();
        }
    }
}